﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

	public CharacterBase _character;
	[HideInInspector]
	public CharacterBase character;
	public Rigidbody2D characterRig;

	public Transform ShawDow;
	private Vector3 temp;
	private bool ableToScore = false;
	private Vector3 TopScreenPos
	{
		get { return new Vector3( Camera.main.ScreenToWorldPoint (Vector3.zero).x, Camera.main.ScreenToWorldPoint (new Vector3(0,Screen.height,0)).y,0);}
	}
	private float leftScreenPos
	{
		get { return new Vector2 (Camera.main.ScreenToWorldPoint (Vector2.zero).x, 0).x;}
	}
	private float rightScreenPos
	{
		get{ return new Vector2 (Camera.main.ScreenToWorldPoint (new Vector2 (Screen.width, 0)).x, 0).x;}
	}
	private void Start()
	{
		if (!ShawDow)
			ShawDow = BattleGroundHelper.instance.transform.GetChild (1);
	}
	private float inPutPosY;
	private float screenPos
	{
		get { return Camera.main.ScreenToWorldPoint (new Vector3 (0, Screen.height * 0.75f, 0)).y;}
	}
	private void Update()
	{
		if (!GameManager.IsPlaying)
			return;
		if (Input.GetMouseButtonDown (0)&&TimeSlider.IsCanPlay) {
			inPutPosY = Camera.main.ScreenToWorldPoint (Input.mousePosition).y;
			if (inPutPosY < screenPos) {
				OnTouchAction ();
				AudioManager.PlayOneShoot (1);
			}
		}
		CheckPosition ();
		BallController ();
		ShadowSetting ();
	}
	Vector2 vel;
	private void BallController()
	{
		if (characterRig != null) {
			vel.x = characterRig.velocity.x * 0.99f;
			vel.y = characterRig.velocity.y;
			characterRig.velocity = vel;
		}
	}
	private void OnTouchAction()
	{
		characterRig.AddForce (new Vector2 (50*LevelManager.basketDirection, 400));

	}
	private void CheckPosition()
	{
		if (transform.position.x < leftScreenPos) {
			transform.position = new Vector2 (rightScreenPos, transform.position.y);
		} else if (transform.position.x > rightScreenPos) {
			transform.position = new Vector2 (leftScreenPos, transform.position.y);
		}
	}
	public void Wait()
	{
		characterRig.isKinematic = true;
		transform.position = Vector3.zero;
		if (character != null) {
			character.Recycle ();
		}
		ShawDow.gameObject.SetActive (false);
	}
	public void PlayGame()
	{
		transform.position = Vector3.zero;
		characterRig.isKinematic = false;
		characterRig.velocity = Vector3.zero;
		characterRig.AddTorque (5);
		character = _character.Spawn (transform,transform.position);
		character.SetDisplay ();
	}
	private void OnTriggerEnter2D(Collider2D col)
	{
		if (col.CompareTag ("scoreside")) {
			if (transform.position.y > col.transform.position.y)
				ableToScore = true;
		}
	}
	private void OnTriggerExit2D(Collider2D col)
	{
		if (col.CompareTag ("scoreside")) {
			if (ableToScore) {
				ableToScore = false;
				if (transform.position.y < col.transform.position.y) {
					CameraController.Shake ();
					//Handheld.Vibrate ();
					LevelManager.instance.SpawnBasket (true);
					TimeSlider.Reset ();
					TestEfxScript.SpawnBezier (col.transform.position);
					UiManager.SetScoreText (true);
					AudioManager.PlayOneShoot (3);
					}
			}
		} 
	}
	private void OnCollisionEnter2D(Collision2D col)
	{
		if (col.gameObject.CompareTag ("groundside")) {
			AudioManager.PlayOneShoot (2);
		}
	}
	private void ShadowSetting()
	{
		if (GameManager.IsPlaying) {
			if (!ShawDow.gameObject.activeSelf)
				ShawDow.gameObject.SetActive (true);
			temp = GameManager.Player.character.transform.position;
			temp.y = -4;
			ShawDow.position = temp;
			var distanceChar = Vector3.Distance (transform.position, TopScreenPos);
			var distanceShadow = Vector3.Distance (ShawDow.position, TopScreenPos);
			var ratio = distanceChar / distanceShadow;
			ShawDow.transform.localScale = ratio * Vector3.one;
		} else {
			if (ShawDow)
				ShawDow.gameObject.SetActive (false);
		}
	}
}
